package com.ouc.dataMange.controller;

import java.io.File;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ouc.common.config.OUCAjaxResult;
import com.ouc.common.utils.excel.ExcelUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ouc.common.annotation.Log;
import com.ouc.common.core.controller.BaseController;
import com.ouc.common.core.domain.AjaxResult;
import com.ouc.common.enums.BusinessType;
import com.ouc.dataMange.domain.SysDataPort;
import com.ouc.dataMange.service.ISysDataPortService;
import com.ouc.common.utils.poi.ExcelUtil;
import com.ouc.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * SysDataPortController
 *
 * @author ouc
 * @date 2025-08-04
 */
@RestController
@RequestMapping("/dataMange/SysDataPort")
public class SysDataPortController extends BaseController {
  @Autowired
  private ISysDataPortService sysDataPortService;

  /**
   * 查询SysDataPort列表
   */
//    @PreAuthorize("@ss.hasPermi('dateMange:SysDataPort:list')")
  @GetMapping("/list")
  public TableDataInfo list(SysDataPort sysDataPort) {
    startPage();
    List<SysDataPort> list = sysDataPortService.selectSysDataPortList(sysDataPort);
    return getDataTable(list);
  }

  /**
   * 导出SysDataPort列表
   */
//    @PreAuthorize("@ss.hasPermi('dateMange:SysDataPort:export')")
  @Log(title = "SysDataPort", businessType = BusinessType.EXPORT)
  @RequestMapping("/export")
  public void export(HttpServletResponse response, SysDataPort sysDataPort) {
    List<SysDataPort> list = sysDataPortService.selectSysDataPortList(sysDataPort);
    ExcelUtil<SysDataPort> util = new ExcelUtil<SysDataPort>(SysDataPort.class);
    util.exportExcel(response, list, "SysDataPort数据");
  }

  /**
   * 获取SysDataPort详细信息
   */
  @PreAuthorize("@ss.hasPermi('dateMange:SysDataPort:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Integer id) {
    return success(sysDataPortService.selectSysDataPortById(id));
  }

  /**
   * 新增SysDataPort
   */
  @PreAuthorize("@ss.hasPermi('dateMange:SysDataPort:add')")
  @Log(title = "SysDataPort", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody SysDataPort sysDataPort) {
    return toAjax(sysDataPortService.insertSysDataPort(sysDataPort));
  }

  /**
   * 修改SysDataPort
   */
  @PreAuthorize("@ss.hasPermi('dateMange:SysDataPort:edit')")
  @Log(title = "SysDataPort", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody SysDataPort sysDataPort) {
    return toAjax(sysDataPortService.updateSysDataPort(sysDataPort));
  }

  /**
   * 删除SysDataPort
   */
  @PreAuthorize("@ss.hasPermi('dateMange:SysDataPort:remove')")
  @Log(title = "SysDataPort", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable Integer[] ids) {
    return toAjax(sysDataPortService.deleteSysDataPortByIds(ids));
  }

  /**
   * 导入（上传excel文件导入数据库）
   */
  @ResponseBody
  @RequestMapping("/uploadData")
  public OUCAjaxResult uploadData(MultipartFile file) {
    try {
      String fileDir = "E:\\mydatabase\\temp\\";
      File newFile = new File(fileDir);
      if (!newFile.exists()) {
        newFile.mkdirs();
      }
      String[] split = file.getOriginalFilename().split("/");
      String fileName = split[split.length - 1];
      file.transferTo(new File(fileDir, fileName));
      File excelFile = new File(fileDir + fileName);

      List<SysDataPort> portList = ExcelUtils.readFile(excelFile, SysDataPort.class);
      for (SysDataPort dataPort : portList) {
        sysDataPortService.insertSysDataPort(dataPort);
      }
      return OUCAjaxResult.ok("已插入DataPort数据=" + portList.size());
    } catch (Exception e) {
      return OUCAjaxResult.error(e.toString());
    }
  }


}
